package org.hjl.commonutils.date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间转换工具
 *
 * @author hjl
 */

public class LocalDateUtil {

    private final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    private final static String DATE_PATTERN = "yyyy-MM-dd";

    /**
     * Date转换为LocalDateTime
     */
    public static LocalDateTime date2LocalDateTime(Date date) {
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        // atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
        return instant.atZone(zoneId).toLocalDateTime();
    }

    /**
     * LocalDateTime转换为Date
     */
    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        if (localDateTime == null) {
            localDateTime = LocalDateTime.now();
        }
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static String localDateTime2StringFormat(LocalDateTime localDateTime, String pattern) {
        if (null == pattern || pattern.trim().isEmpty()) {
            pattern = DATE_TIME_PATTERN;
        }
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(fmt);
    }

    public static LocalDateTime string2LocalDateTimeFormat(String date, String pattern) {
        if (null == pattern || pattern.trim().isEmpty()) {
            pattern = DATE_TIME_PATTERN;
        }
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(date, fmt);
    }

    public static String localDate2StringFormat(LocalDate localDate, String pattern) {
        if (null == pattern || pattern.trim().isEmpty()) {
            pattern = DATE_PATTERN;
        }
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
        return localDate.format(fmt);
    }

    /**
     * 将字符串时间类型，根据指定格式转换为LocalDate
     *
     * @param date    时间
     * @param pattern 格式化格式
     * @return LocalDate
     */
    public static LocalDate string2LocalDateFormat(String date, String pattern) {
        if (null == pattern || pattern.trim().isEmpty()) {
            pattern = DATE_PATTERN;
        }
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(date, fmt);
    }

    /**
     * 计算两个时间的秒数
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return 相差秒数
     */
    public static long betweenTimeToSecond(LocalDateTime beginTime, LocalDateTime endTime) {
        return Duration.between(beginTime, endTime).toMillis() / 1000;
    }

    public static String getDatePoor(LocalDateTime endDate, LocalDateTime startDate) {
        //每天毫秒数
        long nd = 1000 * 24 * 60 * 60;
        //每小时毫秒数
        long nh = 1000 * 60 * 60;
        //每分钟毫秒数
        long nm = 1000 * 60;

        Duration duration = Duration.between(startDate, endDate);
        // 获得两个时间的毫秒时间差异
        long diff = duration.toMillis();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;

        long time = day / 30;
        if (time >= 12) {
            return "一年前";
        } else if (time >= 1) {
            return time + "个月前";
        } else if (day >= 1) {
            return day + "天前";
        } else {
            return hour + "小时" + min + "分钟前";
        }

    }

}
